package org.lzy.shop.viewmodel

import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.response.CommunityPostResponse

class CommunityViewModel : BaseViewModel() {

    val communityPostsLiveData = MutableLiveData<List<CommunityPostResponse>>()
    val errorLiveData = MutableLiveData<String>()

    /**
     * 获取社区推荐帖子
     */
    suspend fun getCommunityPosts(page: Int, pageSize: Int) {
        withContext(Dispatchers.IO) {
            try {
                // 实际项目中应该调用API
                // val response = KtorApiService.getCommunityPosts(page, pageSize)
                // communityPostsLiveData.postValue(response.data)

                // 模拟数据
                val mockPosts = generateMockPosts()
                communityPostsLiveData.postValue(mockPosts)
            } catch (e: Exception) {
                errorLiveData.postValue(e.message ?: "加载失败")
            }
        }
    }

    /**
     * 获取关注的帖子
     */
    suspend fun getFollowPosts(page: Int, pageSize: Int) {
        withContext(Dispatchers.IO) {
            try {
                // 实际项目中应该调用API
                // val response = KtorApiService.getFollowPosts(page, pageSize)
                // communityPostsLiveData.postValue(response.data)

                // 模拟数据
                val mockPosts = generateMockFollowPosts()
                communityPostsLiveData.postValue(mockPosts)
            } catch (e: Exception) {
                errorLiveData.postValue(e.message ?: "加载失败")
            }
        }
    }

    /**
     * 切换点赞状态
     */
    suspend fun toggleLike(postId: String, isLiked: Boolean) {
        withContext(Dispatchers.IO) {
            try {
                // 实际项目中应该调用API
                // KtorApiService.toggleLike(postId, isLiked)
            } catch (e: Exception) {
                errorLiveData.postValue(e.message ?: "操作失败")
            }
        }
    }

    /**
     * 生成模拟推荐帖子数据
     */
    private fun generateMockPosts(): List<CommunityPostResponse> {
        val posts = mutableListOf<CommunityPostResponse>()

        for (i in 1..10) {
            posts.add(CommunityPostResponse(
                id = "post_$i",
                userId = "user_${i % 5 + 1}",
                userName = "用户${i % 5 + 1}",
                userAvatar = "https://picsum.photos/200/200?random=$i",
                content = "这是一个推荐商品的帖子，分享我的购物体验 #好物推荐 #购物分享",
                images = listOf(
                    "https://picsum.photos/400/400?random=${i*10}",
                    "https://picsum.photos/400/400?random=${i*10+1}"
                ),
                likeCount = (100..500).random(),
                commentCount = (20..100).random(),
                shareCount = (5..50).random(),
                isLiked = false,
                createTime = System.currentTimeMillis() - (i * 3600000)
            ))
        }

        return posts
    }

    /**
     * 生成模拟关注帖子数据
     */
    private fun generateMockFollowPosts(): List<CommunityPostResponse> {
        val posts = mutableListOf<CommunityPostResponse>()

        for (i in 1..8) {
            posts.add(CommunityPostResponse(
                id = "follow_post_$i",
                userId = "follow_user_$i",
                userName = "关注用户$i",
                userAvatar = "https://picsum.photos/200/200?random=100$i",
                content = "刚买的新品，质量真的不错！推荐给大家 #新品体验",
                images = listOf(
                    "https://picsum.photos/400/400?random=200$i"
                ),
                likeCount = (50..300).random(),
                commentCount = (10..50).random(),
                shareCount = (2..30).random(),
                isLiked = false,
                createTime = System.currentTimeMillis() - (i * 7200000)
            ))
        }

        return posts
    }
}